import { ArmorConfig } from "../config/ArmorConfig";
import { WeaponConfig } from "../config/WeaponConfig";
import { AttrChangeEnum, ItemAttrConfig, UnitAttrStruct } from "../types/L_Type";
import ArrayX from "./L_Array";
import L_Run from "./L_Run";



/** 属性操作工具类 */
export default class L_AttrControlUtil{
 
  /**
   * 属性合并
   * @param origin 原始属性对象
   * @param extra 增加/减少的属性对象
   * @param mergeType 合并方式 NumberChangeEnum枚举增加减少
   * @param mulVal incrAttrs属性的增加倍率 方便某些情况的处理
   * @returns 
   */
  static mergeField(origin:any,extra:any,mergeType:AttrChangeEnum,mulVal?:number):any{
    if(extra==undefined){
      return origin
    }
    let incFix:number=(mergeType==AttrChangeEnum.INCREASE)?1:-1;
    incFix=incFix*(mulVal || 1)
    for(let k in extra){
        let ov=origin[k] ;
        let av=extra[k] ;
        let typename=typeof av;
        if(typename=="object" && ov==undefined){
          origin[k]={}
          ov=1 //随便设个值只是方便后续快速判断
        }
        if(Array.isArray(av)){
         if(mergeType==AttrChangeEnum.INCREASE){
           ArrayX.pushByDec(origin[k],av)
         }else{
           origin[k]=ArrayX.removeByDec(origin[k],av);
         }
        }else if(typename=="object" && ov!=null){
          L_AttrControlUtil.mergeField(origin[k],av,mergeType);
        }else if(ov==null){
          origin[k]=incFix*av;
        }else{
          origin[k]=ov+incFix*av;
        }
    }
    return origin
  }


  /**
   * 深拷贝对象
   * @param origin 原始对象
   * @returns 
   */
  static deepCloneAttrStruct(origin:UnitAttrStruct):UnitAttrStruct{
    let rs:UnitAttrStruct={};
    if(origin==null || origin==undefined) return rs
    if(origin.attrs!=undefined){
      rs.attrs={}
      for(let k in origin.attrs){
        (rs.attrs as any)[k]=(origin.attrs as any)[k]
      }
    }
    if(origin.dtEnh!=undefined){
      rs.dtEnh={}
      for(let k in origin.dtEnh){
        (rs.dtEnh as any)[k]=(origin.dtEnh as any)[k]
      }
    }
    if(origin.dtRes!=undefined){
      rs.dtRes={}
      for(let k in origin.dtRes){
        (rs.dtRes as any)[k]=(origin.dtRes as any)[k]
      }
    }

    return rs;
  }


  public static getItemConfigAttr(itemTypeId:string):ItemAttrConfig  {
    let configAttr=(WeaponConfig as any)[itemTypeId] || undefined
    if(configAttr!=undefined){
        return configAttr
    }
    configAttr=(ArmorConfig as any)[itemTypeId] || undefined
    //TODO 如果还有其他类型

    if(configAttr==undefined){
      L_Run.errorInfo("发现未配置的物品"+itemTypeId,true)
      return {name:'未配置的物品',lvl:0}
  }

    return configAttr
}

}  


// console.log("????x3")
// let ov={atk:10,def:8,dt_res:{fire:5,dark:-3},kp:[0],eqp:[{name:"qq",v:5}]};
// let iv=  {atk:-3,def:6,hp:20,mp:20,kp:[0,2,3],dt_res:{fire:7,dark:10,ice:11},eqp:[{name:"aa",v:2}]}
// L_FieldControlUtil.mergeField(ov,iv,NumberChangeEnum.INCREASE)
// // console.log(LT_FUNC.getObjJSONStr(ov))
// console.log(ov)



